<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="体格信息id" prop="physicalInfoId">
        <el-input
          v-model="queryParams.physicalInfoId"
          placeholder="请输入体格信息id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊记录id" prop="visitRecordId">
        <el-input
          v-model="queryParams.visitRecordId"
          placeholder="请输入就诊记录id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查时间" prop="examinationTime">
        <el-date-picker clearable
          v-model="queryParams.examinationTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择检查时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="体温" prop="temperature">
        <el-input
          v-model="queryParams.temperature"
          placeholder="请输入体温"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="呼吸" prop="respiration">
        <el-input
          v-model="queryParams.respiration"
          placeholder="请输入呼吸"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="脉搏" prop="pulse">
        <el-input
          v-model="queryParams.pulse"
          placeholder="请输入脉搏"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血压" prop="bloodPressure">
        <el-input
          v-model="queryParams.bloodPressure"
          placeholder="请输入血压"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血糖" prop="bloodSugar">
        <el-input
          v-model="queryParams.bloodSugar"
          placeholder="请输入血糖"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血脂" prop="bloodLipid">
        <el-input
          v-model="queryParams.bloodLipid"
          placeholder="请输入血脂"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入身高"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入体重"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段1" prop="ext1">
        <el-input
          v-model="queryParams.ext1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="ext2">
        <el-input
          v-model="queryParams.ext2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ext4">
        <el-input
          v-model="queryParams.ext4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ext5">
        <el-input
          v-model="queryParams.ext5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sn:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sn:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sn:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['sn:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="体格信息id" align="center" prop="physicalInfoId" />
      <el-table-column label="就诊记录id" align="center" prop="visitRecordId" />
      <el-table-column label="检查时间" align="center" prop="examinationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.examinationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="体温" align="center" prop="temperature" />
      <el-table-column label="呼吸" align="center" prop="respiration" />
      <el-table-column label="脉搏" align="center" prop="pulse" />
      <el-table-column label="血压" align="center" prop="bloodPressure" />
      <el-table-column label="血糖" align="center" prop="bloodSugar" />
      <el-table-column label="血脂" align="center" prop="bloodLipid" />
      <el-table-column label="身高" align="center" prop="height" />
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="扩展字段1" align="center" prop="ext1" />
      <el-table-column label="扩展字段2" align="center" prop="ext2" />
      <el-table-column label="扩展字段3" align="center" prop="ext3" />
      <el-table-column label="扩展字段4" align="center" prop="ext4" />
      <el-table-column label="扩展字段5" align="center" prop="ext5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sn:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sn:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体格信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="就诊记录id" prop="visitRecordId">
          <el-input v-model="form.visitRecordId" placeholder="请输入就诊记录id" />
        </el-form-item>
        <el-form-item label="检查时间" prop="examinationTime">
          <el-date-picker clearable
            v-model="form.examinationTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择检查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体温" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入体温" />
        </el-form-item>
        <el-form-item label="呼吸" prop="respiration">
          <el-input v-model="form.respiration" placeholder="请输入呼吸" />
        </el-form-item>
        <el-form-item label="脉搏" prop="pulse">
          <el-input v-model="form.pulse" placeholder="请输入脉搏" />
        </el-form-item>
        <el-form-item label="血压" prop="bloodPressure">
          <el-input v-model="form.bloodPressure" placeholder="请输入血压" />
        </el-form-item>
        <el-form-item label="血糖" prop="bloodSugar">
          <el-input v-model="form.bloodSugar" placeholder="请输入血糖" />
        </el-form-item>
        <el-form-item label="血脂" prop="bloodLipid">
          <el-input v-model="form.bloodLipid" placeholder="请输入血脂" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="扩展字段1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/sn/info";

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    physicalInfoId: null,
    visitRecordId: null,
    examinationTime: null,
    temperature: null,
    respiration: null,
    pulse: null,
    bloodPressure: null,
    bloodSugar: null,
    bloodLipid: null,
    height: null,
    weight: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询体格信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    physicalInfoId: null,
    visitRecordId: null,
    examinationTime: null,
    temperature: null,
    respiration: null,
    pulse: null,
    bloodPressure: null,
    bloodSugar: null,
    bloodLipid: null,
    height: null,
    weight: null,
    ext1: null,
    ext2: null,
    ext3: null,
    ext4: null,
    ext5: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.physicalInfoId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加体格信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _physicalInfoId = row.physicalInfoId || ids.value
  getInfo(_physicalInfoId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改体格信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.physicalInfoId != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _physicalInfoIds = row.physicalInfoId || ids.value;
  proxy.$modal.confirm('是否确认删除体格信息编号为"' + _physicalInfoIds + '"的数据项？').then(function() {
    return delInfo(_physicalInfoIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sn/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
